﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    public class FieldRepDataAccess : BaseDataAccess<Business.Entities.FieldRep>
    {
        public override GMAC.ReportingSystem.Business.Entities.FieldRep Get(object id)
        {
            ArrayList parameters = new ArrayList() { id };
            DataTable dt = DataAccessComponent.ExecuteSelect("procFieldRepByIdSelect", parameters);
            if (dt != null && dt.Rows.Count > 0)
                return ReadRow(dt.Rows[0]);
            return null;
        }

        public FieldRepCollection GetList()
        {
            DataTable dt = DataAccessComponent.ExecuteSelect("procFieldRepSelect", null);
            return ReadTable(dt) as FieldRepCollection;
        }

        protected override GMAC.ReportingSystem.Business.Entities.FieldRep ReadRow(System.Data.DataRow row)
        {
            FieldRep FieldRep = null;
            if (row != null)
            {
                try
                {
                    FieldRep = new FieldRep();
                    FieldRep.ID = (int)row["ID"];
                    FieldRep.Name = row["Name"].ToString();
                    FieldRep.Type = (GeneralEnumTypes.TypeFieldRep) Enum.Parse(typeof(GeneralEnumTypes.TypeFieldRep),(row["Type"].ToString()));
                    FieldRep.Status = bool.Parse(row["Status"].ToString());
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return FieldRep;
        }

        protected override List<GMAC.ReportingSystem.Business.Entities.FieldRep> ReadTable(System.Data.DataTable table)
        {
            try
            {
                FieldRepCollection FieldReps = new FieldRepCollection();
                FieldRep FieldRep;
                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        FieldRep = ReadRow(row);
                        if (FieldRep != null)
                        {
                            FieldReps.Add(FieldRep);
                        }
                    }
                }
                return FieldReps;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public FieldRepCollection Search(int? id, string name, int? type, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() { id, name, type , paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procFieldRepByFilterSelect", parameters);
                FieldRepCollection result = null;
                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as FieldRepCollection;
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }

        }
         
        
    }
}
